//
//  StatusCell.swift
//  Weibo
//
//  Created by 孙成宇 on 15/11/30.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

//MARK : 定义间距常量

let StatusCellMargin: CGFloat = 15
let StatusCellImageWidth: CGFloat = 40




class StatusCell: UITableViewCell {
 var topConstraints: Constraint?


    //MARK: 添加视图控件
    var status: Status? {
    
        didSet {
       originalView.status = status


            //移除工具条的顶部约束
            topConstraints?.uninstall()
            //设置转发微博的数据模型
            if status?.retweeted_status != nil {
                //就是转发微博
                retweetedView.retweetedStatus = status?.retweeted_status
                //显示转发微博
                retweetedView.hidden = false
                //更新约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.topConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
            } else {
                //隐藏转发微博视图
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.topConstraints = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }


        }
    
    
    }
    
    //MARK : 重写初始化方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        selectionStyle = . None
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK : 设置UI界面
    private func setupUI() {
     
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
      
        //设置布局
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(contentView)
           
            
        }

retweetedView.snp_makeConstraints { (make) -> Void in
    make.left.equalTo(contentView)
    make.top.equalTo(originalView.snp_bottom)
        }

        
    //底部视图的约束
        bottomView.snp_makeConstraints { (make) -> Void in
         self.topConstraints  =  make.top.equalTo(retweetedView.snp_bottom).constraint
           
            make.left.right.equalTo(contentView)
            
            make.height.equalTo(40)
            
        }
        //给contentView添加约束
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            
            make.bottom.equalTo(bottomView.snp_bottom)
        }
        
        
    
    }
    //MARK : 懒加载视图
    private lazy var retweetedView : StatusRetweetView = StatusRetweetView()

    private lazy var originalView: StatusOriginalView = StatusOriginalView()
    
    private lazy var bottomView : StatusCellBottomView = StatusCellBottomView()

}
